#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify
from werkzeug.exceptions import BadRequest

from controller.experimentController import ExperimentController
from service.experimentService import ExperimentService
from utils import commons
from utils.JwtMiddleware import TokenRequire
from utils.myLogging import logger
from utils.responseParser import ResponseParser
from utils.response_code import RET


# 修改实验信息
class ExperimentUpdateResource(Resource):
    @classmethod
    @TokenRequire
    def update(cls, experiment_id):
        if not experiment_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('experiment_name', location='form', required=False, help='experiment_name参数类型不正确或缺失')
        parser.add_argument('classroom', location='form', required=False, help='classroom参数类型不正确或缺失')
        parser.add_argument('description', location='form', required=False, help='description参数类型不正确或缺失')
        parser.add_argument('start_time', location='form', required=False, help='start_time参数类型不正确或缺失')
        parser.add_argument('end_time', location='form', required=False, help='end_time参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['experiment_id'] = experiment_id

        res = ExperimentController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])


# 发布实验
class ExperimentAddResource(Resource):
    @classmethod
    @TokenRequire
    def post(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('experiment_name', location='form', required=False, help='experiment_name参数类型不正确或缺失')
        parser.add_argument('lesson_id', location='form', required=False, help='lesson_id参数类型不正确或缺失')
        parser.add_argument('teacher_id', location='form', required=False, help='teacher_id参数类型不正确或缺失')
        parser.add_argument('classroom', location='form', required=False, help='classroom参数类型不正确或缺失')
        parser.add_argument('description', location='form', required=False, help='description参数类型不正确或缺失')
        parser.add_argument('start_time', location='form', required=False, help='start_time参数类型不正确或缺失')
        parser.add_argument('end_time', location='form', required=False, help='end_time参数类型不正确或缺失')
        try:
            data = parser.parse_args()
            data = commons.put_remove_none(**data)
            res = ExperimentService.add(**data)
            res.get('data') == res.get('data').update(data)
            if res.get("code") != RET.OK:
                logger.error(res.get("data").get("error"))
                return jsonify({
                    "code": res.get("code"),
                    "error": res.get("data").get("error"),
                    "message": "发布实验失败",
                })
            logger.info(f"user {res.get('data').get('OrderID')} add success")
            return jsonify({
                "code": RET.OK,
                "message": "发布实验成功",
                "data": res.get('data')['experiment_id'],
            })
        except BadRequest as e:
            logger.error(str(e))
            return jsonify(ResponseParser.parse_param_error(error=str(e)))
        except Exception as e:
            logger.warning(str(e))
            return jsonify(ResponseParser.parse_unknown_error(error=str(e)))